Today I'm going to introduce the if then statement or more generally known as conditional statements.
So something has to be true in order to execute a block of code.
I have a flat terrain here.
There's nothing in this world.
It's just going to be a demo.
So we'll go ahead and get a part.
I want the block, right?
So get a block and let's go to this block.
Take a look at the size.
Open up size.
It's four one and two on the X, Y and Z respectively.
We'll go ahead and remember that.
Let's add a script to our part.
Hit the plus sign script and then I'm going to call this if then because we are going to learn about
if Ben's in this script to get rid of this print statement and I'm going to get a variable for my part.
So I'll do script dot parent to reference the part and put it in the part variable.
I'm also going to get two more variables.
I want to get one for the x size of the part.
So I'll say part dot size dot x.
Now in order to set the X, Y and z the size, you need a vector three but you can read the individual
X, Y and Z.
We're just going to use X and Y.
All right.
So say part size, dot y.
Cool.
Now I want to execute code if my X is four studs, which it is.
Right.
So I'm going to do this if statement if I want to do x equals E equals or then print x was the right
size.
X was the right size.
Cool.
Notice that this is equals equals.
That is for comparison double equals single equals is for assignment.
We're putting this value into that variable here is here is comparison.
All right, now I said block a code.
This is a single statement so we can make it a block.
Let's just put more than one print statement in there.
Maybe I'll do this little separator here.
So that looks like a block of code.
You've got three print statements going.
Cool.
You also might notice that we have indents here.
We often indent.
So you have a visual, a visual cue of where the block starts and where the block ends.
Now, in Lua you don't have to have the indent, correct.
In Python you do.
This would ere the actual indents designate the beginning and end of the block.
But we could run this.
No problem.
I don't like that, though.
I'm going to show you a little trick on this script tab.
Go to this format section or selection format document.
Boom.
And that way, you know how to format the document.
It'll do it for you.
If you get a messed up, you should check to see if make sure that some of your ends are in the right
place or you don't have an error.
Let's go and run this and see if X is in fact the right size.
I'm going to go to view output.
I got my output window here, maybe make it a little bigger home play and it should print right away.
And it did.
X is the right size.
Well, we knew that.
We knew it was four.
All right.
Let's just add a little bit to this.
This is not going to be an exhaustive talk on if statements just want to introduce enough so that we
can add to it in our other videos.
So we need this that to be true in order for it to be executed.
In order for the block to be executed, you could write true.
But that's not very interesting because the word true is always going to be true.
All right.
Another thing, control Z.
Control Z.
There we go.
You can you can add more than one thing to test in the overall condition.
So I'll say if X equals equals four and and Y equals equals one.
Right.
And Y is one.
We can check on our size.
But I remember that it was.
So if I have an and here this has to be correct and this has to be correct, right.
So that the overall thing will be true.
Let's say X and Y was the right size.
I won't bother.
Yeah.
Let's make these little bigger.
That IntelliSense gets in the way.
This is the IntelliSense.
It helps you out, but sometimes it gets in your way when you're doing videos.
There we go.
All right.
So let's go ahead and see how this works.
It should print out and it does.
Pretty cool.
All right.
I'm not going to get into negation.
Let's go ahead and do an or though.
What does that mean or means if this is right or this is right.
So we only need one to be right.
Let's say X or Y was the right size.
I'll make that a little bigger.
That's not really not that's not really helping, is it?
Okay, so let's go to change.
X to three making that incorrect.
So if I have an ore statement, this is going to be checked.
If it's false, we're going to check this.
If either one of them is true, the overall thing will be true because we're doing it or so this will
still work.
It's an ore and would not.
So if we do, let's try.
And.
X and Y.
This is not going to work.
We're not going to get the print out.
We did not.
But all will work.
You can trust me on that one.
All right.
I want to show you something else.
If that if statement fails, you can have something called an LL statement, and we'll just say.
Print.
The numbers were not correct.
Now, this is going to print, right?
We're going to go in here.
We're going to see that that doesn't work because our X is wrong.
And then this is going to print out, let's do it.
The numbers were not correct.
All right.
And I want to show you something else, too.
For now, I'm just going to move this down.
Maybe we want to check to see if this is failing or that's failing in our statement so I can do something
called LCF.
So no space.
If you put a space in there, you're essentially putting another block of if inside an else.
So we'll say elseif x equals equals three.
Then print x was right.
I will say lcf.
Y equals equals one.
Then print y was right.
Now the first correct condition we get, we're going to execute the block.
Then we're going to leave this entire compound statement.
Only one of these is going to fire.
So this is wrong.
We're going to come down here.
We're going to find out this is wrong because X is actually four.
We're going to see that this is true and we're going to see y was right.
And it's got in print.
Why was right.
All righty.
So if this one what if we have this?
This is correct.
Let's make this correct.
Let's make them all correct.
Right.
So this is this is going to be true.
This is going to be true and this is going to be true.
What's going to print out while it's going to be the first one?
These will not be tested and the else, of course, will be skipped over.
Let's go ahead and try it.
A few output make that bigger.
And I had to play from here.
Look at that, the first one.
Now, if you take a computer science class, this is going to get really, really in-depth.
They're going to do something called truth tables, the Morgan's Laws.
These are going to get big and scary.
You're going to have stuff like the counter positive and all this other stuff here.
We just want to know enough to get things working.
And I want you to be familiar enough so that when you see it in the code in the preceding videos, you're
not freaked out saying, Oh man, what's that?
So good luck with the if statement.
It's a valuable tool and we'll do something a little more interesting with it coming up in the next
videos.